/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.app.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xw.app.dao.AcctUserDAO;
import com.xw.app.model.AcctRole;
import com.xw.app.model.AcctUser;
import com.xw.app.model.SysMenu;
import com.xw.app.service.AcctUserService;
import com.xw.framework.base.service.BaseServiceImpl;
import com.xw.framework.vo.UserMenu;

/**
 * The Class AcctUserServiceImpl.
 *
 * @author Xi Wei
 */
@Service
@Transactional
public class AcctUserServiceImpl extends BaseServiceImpl<AcctUser,Long> implements AcctUserService {

	@Autowired
	private AcctUserDAO acctUserDAO;
	
	/* (non-Javadoc)
	 * @see com.xw.app.service.AcctUserService#getUserByLoginName(java.lang.String)
	 */
	@Override
	public AcctUser getUserByLoginName(String loginName) {
		return acctUserDAO.findByLoginName(loginName);
	}
	
	/* (non-Javadoc)
	 * @see com.xw.app.service.AcctUserService#getMenuListForUser(com.xw.app.model.AcctUser)
	 */
	@Override
	public List<UserMenu> getMenuListForUser(AcctUser user){
		Map<Long,UserMenu> menuMap = Maps.newLinkedHashMap();
		List<AcctRole> roleList = user.getRoleList();
		
		for(AcctRole role:roleList){
			List<SysMenu> menuList = role.getMenuList();
			for(SysMenu menu : menuList){
				UserMenu userMenu = new UserMenu();
				userMenu.setId(menu.getId());
				userMenu.setMenuCode(menu.getMenuCode());
				userMenu.setMenuIcon(menu.getMenuIcon());
				userMenu.setMenuLevel(menu.getMenuLevel());
				userMenu.setMenuName(menu.getMenuName());
				userMenu.setMenuOrder(menu.getMenuOrder());
				userMenu.setMenuType(menu.getMenuType());
				userMenu.setMenuUrl(menu.getMenuUrl());
				if(menu.getParent() != null){
					userMenu.setHasParent(true);
					userMenu.setParentId(menu.getParent().getId());
				}
				
				menuMap.put(menu.getId(), userMenu);
			}			
		}
		
		for(UserMenu userMenu:menuMap.values()){
			if(userMenu.isHasParent()){
				menuMap.get(userMenu.getParentId()).getSubMenuList().add(userMenu);
			}
		}
		List<UserMenu> userMenuList = Lists.newArrayList();
		for(UserMenu userMenu:menuMap.values()){
			if(!userMenu.isHasParent()){
				userMenuList.add(userMenu);
			}
		}
		return userMenuList;
		
	}
	
	/* (non-Javadoc)
	 * @see com.xw.app.service.AcctUserService#getUserAndRolesbyId(java.lang.Long)
	 */
	@Override
	public AcctUser getUserAndRolesbyId(Long id) {
		AcctUser user = acctUserDAO.findOne(id);
		List<AcctRole> roleList = user.getRoleList();
		for (AcctRole role : roleList) {
			List<SysMenu> menuList = role.getMenuList();
			for (SysMenu menu : menuList) {
				getSubMenu(menu);
			}
		}
		return user;
	}
	
	private void getSubMenu(SysMenu menu){
		List<SysMenu> menuList = menu.getSubMenuList();
		for(SysMenu subMenu : menuList){
			getSubMenu(subMenu);
		}
	}
}
